(ns Client.client
  "The mediator between the GUI and the Server"
  (:gen-class)
  (:import (java.net Socket ServerSocket)
            (java.io PrintWriter BufferedReader InputStream InputStreamReader))
  (:use clojure.contrib.duck-streams
        clojure.contrib.str-utils
    ))

(def *port* 8000)
(def *host* "localhost")
;-------------------------------------------------------------------------------
(defn search
  "pruebas"
  [text field]
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
      (.println output (str "search" "_" text "_" field "_"))
      (.flush output)
      (read-string (.readLine input)))
      ))
;-------------------------------------------------------------------------------
(defn create-register-rbf
  "pruebas"
  [nam location size smoking rate date owner]
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
		  (.println output (str "create-register-rbf" "_" nam "_"
		    location "_" size "_" smoking "_" rate "_" date "_" owner "_"))
		  (.flush output)
		  (if (.equals (.readLine input) "ok")
		   nil -1)
		  ;(read-string (.readLine input))
      )))
;-------------------------------------------------------------------------------
(defn delete-register-rbf
  "pruebas"
  [index]
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
		  (.println output (str "delete-register-rbf" "_" index "_"))
		  (.flush output)
		  (if (.equals (.readLine input) "ok")
		   nil -1)
		  ;(read-string (.readLine input))
      )))
;-------------------------------------------------------------------------------
(defn book-room-rbf
  "pruebas"
  [index new-owner]
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
		  (.println output (str "book-room-rbf" "_" index "_" new-owner "_"))
		  (.flush output)
		  (if (.equals (.readLine input) "ok")
		   nil -1)
		  ;(read-string (.readLine input))
      )))

;-------------------------------------------------------------------------------
(defn edit-register-rbf
  "pruebas"
  [index nam location size smoking rate date owner]
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
		  (.println output (str "edit-register-rbf" "_" index "_" nam "_"
		    location "_" size "_" smoking "_" rate "_" date "_" owner "_"))
		  (.flush output)
		  (if (.equals (.readLine input) "ok")
		   nil -1)
		  ;(read-string (.readLine input))
      )))

;-------------------------------------------------------------------------------
(defn return-index
  "pruebas"
  [nam location size smoking rate date owner]
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
		  (.println output (str "return-index" "_" nam "_"
		    location "_" size "_" smoking "_" rate "_" date "_" owner "_"))
		  (.flush output)
		  (read-string (.readLine input)))
      ))

;-------------------------------------------------------------------------------
(defn get-records
  "pruebas"
  []
(let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
      (.println output "get-records")
      (.flush output)
      (read-string (.readLine input)))
      ))

;-------------------------------------------------------------------------------

(defn test-connection
  "prueba"
  []
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
      (.println output "hola_adios")
      ;(.println output "noma")
      (.flush output)
      (println (.readLine input))
      )))
;-------------------------------------------------------------------------------
(defn test-connection-2
  "prueba"
  []
  (let [
    socket (Socket. *host* *port*)
    ]
    (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
		            output (PrintWriter. (.getOutputStream socket))]
      (.println output "waka")
      (.flush output)
      (println (.readLine input))
      )))
;-------------------------------------------------------------------------------
;(search-client "N" :smoking)
;(search-client "e" :location)
;(search-client "E" :name)
;(search-client "1" :owner)
;(create-register-client "Mi Hotel1" "Mexico DF" "2" "N" "$200.00" "2010/03/13" "")
;(delete-register-client 0)
;(book-room-client 0 "11111111")
;(edit-register-client 0 "Mi Hotel2" "Mexico DF" "2" "N" "$200.00" "2010/03/13" "")
;(return-index-client "Grandview" "EmeraldCity" "6" "N" "$100.00" "2003/02/08" "")
;(test-connection)
;(.replace (get-records-client) \"\" "") 
;(split-sentence (get-records-client))
;(nth (read-string (get-records-client)) 4)
;(nth (get-records-client) 0)
;(get-records-client)
;(return-index-client "Grandview" "EmeraldCity" "6" "N" "$100.00" "2003/02/08" "")(return-index-client "Grandview" "EmeraldCity" "6" "N" "$100.00" "2003/02/08" "") 

